package com.whsr.readservice.dao;

import com.whsr.readservice.dto.ArticleRequest;
import com.whsr.readservice.vo.Article;
import com.whsr.readservice.vo.PageConfigGroup;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface ArticleMapper {

    @Insert("<script>" +
            "insert into article (book_id, book_name," +
            " book_cover, book_author, status," +
            " words, book_intro, book_class," +
            " chapter_count, category_id, create_time," +
            " update_time)" +
            "values" +
            "<foreach collection =\"articles\" item=\"article\" index=\"index\" separator =\",\">" +
            "(#{article.bookId}, #{article.bookName}," +
            " #{article.bookCover}, #{article.bookAuthor}, #{article.status}," +
            " #{article.words}, #{article.bookIntro}, #{article.bookClass}," +
            " #{article.chapterCount}, #{article.categoryId}, now()," +
            " now())" +
            "</foreach>" +
            "</script>")
    int insertArticles(@Param("articles") List<Article> articles);

    @Select("select " +
            " id as id, " +
            " book_id as bookId, " +
            " book_name as bookName, " +
            " book_cover as bookCover, " +
            " book_author as bookAuthor, " +
            " status as status, " +
            " words as words, " +
            " book_intro as bookIntro, " +
            " book_class as bookClass, " +
            " category_id as categoryId, " +
            " data_flag as dataFlag, " +
            " chapter_count as chapterCount, " +
            " create_time as createTime, " +
            " update_time as updateTime " +
            "from article a where a.book_id = #{bookId}")
    Article selectArticleByBookId(@Param("bookId") Integer bookId);

    @Select("<script>" +
            "select " +
            " a.id as id, " +
            " a.book_id as bookId, " +
            " a.book_name as bookName, " +
            " a.book_cover as bookCover, " +
            " a.book_author as bookAuthor, " +
            " a.status as status, " +
            " a.words as words, " +
            " a.book_intro as bookIntro, " +
            " a.book_class as bookClass, " +
            " a.category_id as categoryId, " +
            " a.data_flag as dataFlag, " +
            " a.chapter_count as chapterCount, " +
            " a.create_time as createTime, " +
            " a.update_time as updateTime " +
            "from article a " +
            "where 1=1 " +
            "<when test='bookId!=null'>" +
            " and a.book_id = #{bookId} " +
            "</when>" +
            "<when test='bookName!=null'>" +
            " and a.book_name = #{bookName} " +
            "</when>" +
            "<when test='bookAuthor!=null'>" +
            " and a.book_author = #{bookAuthor} " +
            "</when>" +
            "<when test='status!=null'>" +
            " and a.status = #{status} " +
            "</when>" +
            "<when test='words!=null'>" +
            " and a.words = #{words} " +
            "</when>" +
            "<when test='bookClass!=null'>" +
            " and a.book_class = #{bookClass} " +
            "</when>" +
            "<when test='dataFlag!=null'>" +
            " and a.data_flag = #{dataFlag} " +
            "</when>" +
            "</script>")
    List<Article> selectArticlesByArticle(Article article);


    @Select("<script>" +
            "select " +
            " a.id as id, " +
            " a.book_id as bookId, " +
            " a.book_name as bookName, " +
            " a.book_cover as bookCover, " +
            " a.book_author as bookAuthor, " +
            " a.status as status, " +
            " a.words as words, " +
            " a.book_intro as bookIntro, " +
            " a.book_class as bookClass, " +
            " a.category_id as categoryId, " +
            " a.data_flag as dataFlag, " +
            " a.chapter_count as chapterCount, " +
            " a.create_time as createTime, " +
            " a.update_time as updateTime," +
            " d.id as parentCategoryId," +
            " d.name as parentCategoryName " +
            "FROM article a, category c, category d" +
            " where 1=1 " +
            " and a.category_id = c.id" +
            " and c.p_id = d.id" +
            "<when test='bookId!=null'>" +
            " and a.book_id = #{bookId} " +
            "</when>" +
            "<when test='bookName!=null'>" +
            " and a.book_name like concat(concat('%',#{bookName}),'%') " +
            "</when>" +
            "<when test='dataFlag!=null'>" +
            " and a.data_flag = #{dataFlag} " +
            "</when>" +
            "<when test='categoryId!=null'>" +
            " and a.category_id = #{categoryId} " +
            "</when>" +
            "<when test='parentCategoryId!=null'>" +
            " and d.id = #{parentCategoryId} " +
            "</when>" +
            "</script>")
    List<ArticleRequest> selectArticlesByUnion(ArticleRequest article);

    @Update("update article set data_flag = #{dataFlag}, update_time = now() where book_id = #{bookId}")
    int changeArticleDataFlagByBookId(@Param("bookId") Integer bookId, @Param("dataFlag") Integer dataFlag);


    @Select("select book_id as id, book_name as value from article where book_name like CONCAT('%',#{name},'%')")
    List<Map<String, Object>> queryArtByName(String name);

    @Select("select " +
            " book_id as bookId, " +
            " book_name as bookName, " +
            " book_cover as bookCover, " +
            " book_author as bookAuthor, " +
            " status as status, " +
            " words as words, " +
            " book_intro as bookIntro, " +
            " book_class as bookClass, " +
            " category_id as categoryId, " +
            " chapter_count as chapterCount " +
            "from article a where a.book_id = #{bookId}")
    Article selectArticleByAppBookId(@Param("bookId") Integer bookId);


    @Delete("delete from article where book_id = #{bookId}")
    int deleteArticleByBookId(@Param("bookId") Integer bookId);
}
